package org.linlinjava.litemall.db.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.linlinjava.litemall.db.dao.CouponUserMapper;
import org.linlinjava.litemall.db.models.CouponUser;
import org.linlinjava.litemall.db.service.CouponUserService;
import org.linlinjava.litemall.db.util.CouponUserConstant;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class CouponUserServiceImpl extends ServiceImpl<CouponUserMapper, CouponUser>
    implements CouponUserService {

    @Override
    public Integer countCoupon(Long couponId) {
        return baseMapper.selectCount(new LambdaQueryWrapper<CouponUser>().eq(CouponUser::getCouponId, couponId));
    }

    @Override
    public Integer countUserAndCoupon(Long userId, Long couponId) {
        return baseMapper.selectCount(new LambdaQueryWrapper<CouponUser>()
                .eq(CouponUser::getCouponId, couponId).eq(CouponUser::getUserId, userId));
    }

    @Override
    public IPage<CouponUser> queryList(Long userId, Long couponId, Short status, Integer pageNo, Integer pageSize) {
        Page<CouponUser> page = new Page<>(pageNo, pageSize);
        QueryWrapper<CouponUser> query = new QueryWrapper<>();
        query.eq(userId != null, "user_id", userId);
        query.eq(couponId != null, "coupon_id", couponId);
        query.eq(status != null, "status", status);
        return baseMapper.selectPage(page, query);
    }

    @Override
    public List<CouponUser> queryAll(Long userId, Long couponId) {
        QueryWrapper<CouponUser> query = new QueryWrapper<>();
        query.eq(userId != null, "user_id", userId);
        query.eq(couponId != null, "coupon_id", couponId);
        query.eq("status", CouponUserConstant.STATUS_USABLE);
        query.orderByDesc("add_time");
        return baseMapper.selectList(query);
    }

    @Override
    public List<CouponUser> queryAll(Long userId) {
        QueryWrapper<CouponUser> query = new QueryWrapper<>();
        query.eq(userId != null, "user_id", userId);
        query.eq("status", CouponUserConstant.STATUS_USABLE);
        query.orderByDesc("add_time");
        return baseMapper.selectList(query);
    }

    @Override
    public CouponUser queryOne(Long userId, Long couponId) {
        QueryWrapper<CouponUser> query = new QueryWrapper<>();
        query.eq("user_id", userId);
        query.eq("coupon_id", couponId);
        return baseMapper.selectOne(query);
    }

    @Override
    public List<CouponUser> queryExpired() {
        QueryWrapper<CouponUser> query = new QueryWrapper<>();
        query.eq("status", CouponUserConstant.STATUS_USABLE);
        query.lt("end_time", LocalDateTime.now());
        //example.or().andStatusEqualTo(CouponUserConstant.STATUS_USABLE).andEndTimeLessThan(LocalDateTime.now()).andDeletedEqualTo(false);
        return baseMapper.selectList(query);
    }

    @Override
    public List<CouponUser> findByOid(Long orderId) {
        QueryWrapper<CouponUser> query = new QueryWrapper<>();
        query.eq("order_id", orderId);
        return baseMapper.selectList(query);
    }
}
